const fs = require("fs");
const path = require("path");
const client = require("scp2");

const serverConfig = {
    host: '10.10.12.224',
    port: 22,
    username: 'root',
    password: 'Kingbase2023!'
};

const buildDir = "./dist";
const targetDir = "/home/fanyi/apache-tomcat-9.0.98/webapps/online-demo";

const files = fs.readdirSync(buildDir);

console.log("上传文件中...")

files.forEach(file => {
    const filePath = path.join(buildDir, file);
    const stats = fs.statSync(filePath);
    const targetPath = stats.isDirectory() ? `${targetDir}/${file}` : targetDir;
    client.scp(
        filePath,
        {
            ...serverConfig,
            path: targetPath
        },
        err => {
            if(err){
                console.error("Error: " + filePath, err)
            }else {
                console.log("Success: " + filePath)
            }
        }
    )
})